<?php

namespace App\Services;

use App\Models\AreaModel;
use App\Models\CityModel;
use App\Models\ProvinceCodeModel;
use App\Models\ProvinceModel;

class BaseService
{
    public static $proCode = [
        "370000" => [
            "370100" => "济南市",
            "370200" => "青岛市",
            "370300" => "淄博市",
            "370400" => "枣庄市",
            "370500" => "东营市",
            "370600" => "烟台市",
            "370700" => "潍坊市",
            "370800" => "济宁市",
            "370900" => "泰安市",
            "371000" => "威海市",
            "371100" => "日照市",
            "371300" => "临沂市",
            "371400" => "德州市",
            "371500" => "聊城市",
            "371600" => "滨州市",
            "371700" => "菏泽市",
        ]
    ];

    public static $channel_code = [
        "大客户", "业务大客户", "代办", "客户管家"
    ];

    /** Redis实例
     * @param int $db
     * @return \Redis
     */
    public static function getRedis($db=0){
        $Redis = new \Redis();
        $Redis->connect(env('REDIS_HOST'), env('REDIS_PORT'));
        $Redis->auth(env('REDIS_PASSWORD'));
        $Redis->select($db);
        return $Redis;
    }

    /**
     * 通过中文名字反查省份id
     */
    public function getProvinceCodeByName($province_name)
    {
        $query = ProvinceModel::query();
        $query->where("name", $province_name);
        $res = $query->first();
        return empty($res) ? [] : $res->toArray();
    }

    /**
     * 通过城市名称反查城市id和省份id
     */
    public function getCityCodeByName($city_name)
    {
        $query = CityModel::query();
        $query->where("name", $city_name);
        $res = $query->first();
        return empty($res) ? [] : $res->toArray();
    }

    /**
     * area表 获取省份列表
     */
    public function getProvinceList(){
        $res = AreaModel::where("parent_id", 0)->get();
        return empty($res) ? [] : $res->toArray();
    }

    public function getProvinceChildList($parent_id = 0) {
        $query = AreaModel::query();
        $query->where("parent_id", $parent_id);
        $res = $query->get();
        return empty($res) ? [] : $res->toArray();
    }

    /**
     * 模糊搜索省市
     */
    public function getAreaByLike($name) {
        $query = AreaModel::query();
        $query->where("name", "like", "%$name%");
        $res = $query->first();
        return empty($res) ? [] : $res->toArray();
    }
}
